const db= require('../../db/index');
const path=require('path');
let addarticle= (req,res)=>{
    console.log("22222");
    // 1.判断封面是否上传
    if(!req.file || req.file.fieldname!=='cover_img' ) return res.cc('封面是必选项');
    // 收集数据
    const articleInfo ={
        ...req.body,
        cover_img: path.join('/uploads', req.file.filename),
        pub_date: new Date(),
        author_id: req.user.id
    }
    // sql
    const sqlStr=`insert into ev_articles set ?`;
    db.query(sqlStr,articleInfo,(err,results)=>{
        if(err) return res.cc(err);
        if(results.affectedRows!==1) return res.cc('发布文章失败');
        res.cc('发布文章成功',0)
    })
}

module.exports=addarticle;
